package org.ellisande.grid.tags;

import java.util.EnumSet;

import javax.servlet.jsp.JspWriter;

import org.ellisande.grid.tags.support.TagBuilder;
import org.ellisande.tld.annotations.Attribute;
import org.ellisande.tld.annotations.Description;
import org.ellisande.tld.annotations.Info;
import org.ellisande.tld.annotations.Tag;

@Tag
@Info("Provides a list that can contains navigation links. This tag expects navListItems in the body.")
public class NavListTag extends AbstractTag {

	private static final long serialVersionUID = -2849595958365022173L;

	protected static enum Type {
		PLAIN(" "),PILLS("nav-pills"), TABS("nav-tabs");

		private String type;

		private Type(String type) {
			this.type = type;
		}

		public String toString() {
			return type;
		}
		
		public boolean equals(Type type){
			return this.name().equals(type.name());
		}
	}

	@Attribute
	@Description("The type of nav list to be created. Allowed values: pills | tabs | plain. Default: pills.")
	private Type type = Type.PILLS;

	@Attribute
	@Description("Defines whether or not to stack the nav list. Allowed values: true | false. Default: false.")
	private String stacked = "";

	@Override
	public void startTag() throws Exception {
		TagBuilder list = createTagBuilder("ul");
		list.addStyle("nav");
		if(!type.equals(Type.PLAIN)){
			list.addStyle("nav-list");
		}
		list.addStyle(type.toString());
		list.addStyle(stacked);
		
		JspWriter out = pageContext.getOut();
		out.println(list.compileStartTag());
	}

	@Override
	public void endTag() throws Exception {
		TagBuilder list = createTagBuilder("ul");

		JspWriter out = pageContext.getOut();
		out.println(list.compileEndTag());
	}

	@Override
	public void release() {
		super.release();
		type = null;
		stacked = "";
	}

	public void setType(String type) {
		for (Type currentType : EnumSet.allOf(Type.class)) {
			if (currentType.name().equalsIgnoreCase(type)) {
				this.type = currentType;
				return;
			}
		}
	}

	public void setStacked(String stacked) {
		if ("true".equalsIgnoreCase(stacked)) {
			this.stacked = "nav-stacked";
			return;
		}

		this.stacked = "";
	}
}
